﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static OpenTK.Graphics.OpenGL.GL;

namespace ttbit.WorkingSpace
{
    public class MetaInfo
    {
        public override string ToString()
        {
            return "<Meta:" + Type + ">" + srcfile;
        }
        public string Type
        {
            get;
            private set;
        }
        public uint srcfilesize;
        public byte[] srcfilehash;
        public long filetime;
        public string srcfile;
        public string Name
        {
            get
            {
                return System.IO.Path.GetFileNameWithoutExtension(srcfile).ToLower();
            }
        }
        public string Ext
        {
            get
            {
                return System.IO.Path.GetExtension(srcfile).ToLower();
            }
        }
        public void Init(string Type, FileInfo fileinfo)
        {
            this.Type = Type;
            this.srcfile = fileinfo.FullName;
            this.filetime = fileinfo.LastWriteTime.ToFileTime();
            this.srcfilesize = (uint)fileinfo.Length;
            this.srcfilehash = HashTool.CalcHash(fileinfo);
        }

        public bool LoadAndCheck( FileInfo fileinfo)
        {
            var metafile = fileinfo.FullName + ".meta";
            uint filesize = 0;
            byte[] filehash = null;
            using (var fs = System.IO.File.OpenRead(metafile))
            {
                byte[] bufhead = new byte[95];
                fs.Read(bufhead, 0, 95);
                string head = System.Text.Encoding.ASCII.GetString(bufhead);
                var works = head.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                var magic = works[0];
                if (magic != "TT.1")
                    throw new Exception("error meta head.");

                var metatype = works[1];
                this.Type = metatype;
                //if (metatype != Type)
                //    throw new Exception("error type");
                filesize = BitConverter.ToUInt32(Convert.FromHexString(works[2]));

                filehash = Convert.FromHexString(works[3]);
            }

            var newsize = (uint)fileinfo.Length;
            var newhash = HashTool.CalcHash(fileinfo);


            this.srcfile = fileinfo.FullName;
            this.filetime = fileinfo.LastWriteTime.ToFileTime();
            this.srcfilesize = newsize;
            this.srcfilehash = newhash;

            return newsize != filesize || !HashTool.Same(filehash, newhash);
        }
        public void Write(System.IO.Stream stream)
        {
            string typefix = Type;
            if (typefix.Length > 16)
                typefix = typefix.Substring(0, 16);
            while (typefix.Length < 16)
                typefix += ' ';

            //Magic 4字节 +空格+Type 16字节 +空格 + 8字节 hexstr srcsize+ 空格+ 64字节hexstr srchash + 回车
            string outtxt = "TT.1 " + typefix + " " + Convert.ToHexString(BitConverter.GetBytes(this.srcfilesize)) + " " + Convert.ToHexString(this.srcfilehash) + "\n";
            byte[] buf = System.Text.Encoding.ASCII.GetBytes(outtxt);
            if (buf.Length != 96)
                throw new Exception("错误的Meta头");
            stream.Write(buf, 0, buf.Length);
        }
    }
}
